#Copyright 2011 Wesley Crossman - No warranty - Placed under GPLv3 (or later, your choice)
#Arguments: updateinfo.py <version> <copyright year range>
#Example: updateinfo.py 1.0 2011-2012
#
#Assumptions:
#Current path is steeptimer\source
#Copyright is updated in ..\*.{cpp,h}, license.html, main.rc, setup.iss
#Version is updated in main.cpp, TeaAboutDialog.ui, main.rc, setup.iss

import sys, re, os, shutil, zipfile

def replace(file_string, from_re, to):
    with open(file_string, "r", newline="") as file:
        text = file.read()
        text = re.sub(from_re,
                      to,
                      text)
    with open(file_string, "w", newline="") as file:
        file.write(text)

def main():
    version = sys.argv[1]
    version_re = re.compile(r"(\d)\.(\d+)").match(version)
    assert(version_re)
    copyright_range = sys.argv[2]
    assert(re.compile(r"20\d\d(-20\d\d)?$").match(copyright_range))

    os.chdir("..")

    #update copyright in C++ files if changed
    for root, dirs, files in os.walk(".", True):
        dirs[:] = [d for d in dirs if not d.startswith('.')]
        for f in files:
            ext_match = f.endswith(".cpp") or f.endswith(".c") or f.endswith(".h")
            if root.find("\\.") == -1 and not f.startswith("moc_") and ext_match:
                with open(os.path.join(root, f), "r", newline="") as file:
                    text = file.readlines()
                newvalue = " * Copyright (C) " + copyright_range + "  Wesley Crossman\r\n"
                if text[2] != newvalue and text[2].startswith(" * Copyright"):
                    text[2] = newvalue
                    print(f, "set!")
                    with open(os.path.join(root, f), "w", newline="") as file:
                        file.writelines(text)

    replace(r"main.cpp",
            r'(?<=app.setApplicationVersion\(")[0-9.]+',
            version)

    replace(r"resources\license.html",
            r"&copy; [0-9-]+ Wesley Crossman",
            "&copy; " + copyright_range + " Wesley Crossman")
    
    replace(r"TeaAboutDialog.ui",
            r"<string>SteepTimer [0-9.]+</string>",
            "<string>SteepTimer " + version + "</string>")
    
    replace(r"main.rc",
            r'(?<=VALUE "FileVersion", ")[0-9.]+',
            version + '.0')
    replace(r"main.rc",
            r'(?<=VALUE "ProductVersion", ")[0-9.]+',
            version + '.0')
    replace(r"main.rc",
            r'(?<=VALUE "LegalCopyright", "(C) )[0-9-]+',
            copyright_range)
    replace(r"main.rc",
            r"(?<=PRODUCTVERSION )\d,\d,\d,\d",
            version_re.group(1) + "," + version_re.group(2) + ",0,0")
    replace(r"main.rc",
            r"(?<=FILEVERSION )\d,\d,\d,\d",
            version_re.group(1) + "," + version_re.group(2) + ",0,0")
    
    replace(r"source\setup.iss",
            r"(?<=VersionInfoTextVersion=)[0-9.]+",
            version)
    replace(r"source\setup.iss",
            r"(?<=VersionInfoProductVersion=)[0-9.]+",
            version)
    replace(r"source\setup.iss",
            r"(?<=VersionInfoVersion=)[0-9.]+",
            version + ".0.0")
    replace(r"source\setup.iss",
            r"(?<=AppVerName=SteepTimer )[0-9.]+",
            version)
    replace(r"source\setup.iss",
            r"(?<=AppCopyright=Copyright )[0-9-]+",
            copyright_range)
    replace(r"source\setup.iss",
            r"(?<=VersionInfoCopyright=)[0-9-]+",
            copyright_range)
    replace(r"source\setup.iss",
            r"(?<=OutputBaseFilename=SetupSteepTimer-)[0-9.]+",
            version)
    replace(r"source\setup.iss",
            r"(?<=AppVersion=)[0-9.]+",
            version)

    open("version.txt", "w", newline="").write(version)

main()
